package controller;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import net.GooglePageParserRegexp;
import net.GoogleResultItem;
import net.PageFetcher;

public class TestAgent {

	public static void main(String args[]) {
		ArrayList<GoogleResultItem> googleResults = new ArrayList<GoogleResultItem>();
		int GooglePages = 5;
		// Get top 50 results
		String query = "%22java.io.BufferedReader%22+example+OR+java";
		for (int i = 0; i < GooglePages; i++) {
			System.err.println("Getting result in pp.:" + i + "...");
			
			String queryURL = "http://www.google.com/search?q="
					+ query + "&hl=en&start="
					+ (i * 10) + "&sa=N";
			System.out.println(queryURL);
			String content = PageFetcher.fetchURL(queryURL);
			System.err.println("Got it!");
			ArrayList<GoogleResultItem> items = new GooglePageParserRegexp()
					.parse(content, "pp_" + i);
			for (GoogleResultItem googleResultItem : items) {
				if (!googleResults.contains(googleResultItem)) {
					googleResults.add(googleResultItem);
					System.out.println("BackLink:\t"
							+ googleResultItem.getOriginalURL());
					System.out.println("Anchor:\t"
							+ googleResultItem.getAnchorText());
					System.out.println("Summary:\t"
							+ googleResultItem.getSummary());
				}
			}
			System.err.println("Waiting for 5 seconds...");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// Start to fetch target pages
		int num = 0;
		File dir = new File("./test_data/"+query+"/");
		if(!dir.exists()){
			dir.mkdirs();
		}
		ExecutorService executor = Executors.newCachedThreadPool();
		for (GoogleResultItem googleResultItem : googleResults) {			
			executor.execute(new GetCodeSegmentThread(query, googleResultItem, num++));
		}
		executor.shutdown();
	}

}
